import allure
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait

from utils.error_handle import black
from utils.log_util import logger
from utils.utils import Utils


class BasePage:
    def __init__(self, driver: WebDriver = None):
        self.driver = driver

    @black
    def find_ele(self, by, value=None):
        """
        查找元素
        :param by: 定位方式
        :param value: 定位表达式
        :return: 找到的元素
        """
        info = f'查找元素，定位方式为{by},定位表达式为{value}'
        logger.info(info)

        if value == None:
            try:
                ele = self.driver.find_element(*by)
            except Exception as e:
                ele = None
                logger.info(f'元素查找异常,{e}')
        else:
            ele = self.driver.find_element(by, value)
        return ele

    @black
    def find_eles(self, by, value=None):
        """
        查找多个元素
        :param by: 定位方式
        :param value: 定位表达式
        :return: 找到的元素
        """
        if value == None:
            eles = self.driver.find_elements(*by)
        else:
            eles = self.driver.find_elements(by, value)
        return eles
    def quit(self):
        """
        退出方法
        :return:
        """
        self.driver.terminate_app('com.xueqiu.android')

    def swipe(self, start, end):
        """
        滑动
        :return:
        """
        size = self.driver.get_window_size()
        height = size.get('height')
        wight = size.get('width')
        self.driver.swipe(
            start_x=wight*0.5,
            start_y=height*float(start),
            end_x=wight*0.5,
            end_y=height*float(end),
            duration=2000
        )

    def find_swipe(self, name, s, ss, n_max=5):
        """
        滑动查找
        """
        self.wait_im(2)
        for i in range(n_max):
            try:
                ele = self.driver.find_element(AppiumBy.XPATH, f"//*[@text='{name}']")
                return ele
            except Exception as e:
                print(e)
                self.swipe(start=s, end=ss)

    def screen(self):
        """
        截图
        :return:
        """
        screen_path = Utils.save_source_datas('images')
        self.driver.save_screenshot(screen_path)
        logger.info(f'截图保存路径为{screen_path}')
        allure.attach.file(
            screen_path,
            name='元素截图',
            attachment_type=allure.attachment_type.PNG
        )
        return screen_path

    def source(self):
        """
        源码
        :return:
        """
        source_path = Utils.save_source_datas('pagesource')
        with open(source_path, "w", encoding='utf-8') as f:
            f.write(self.driver.page_source)
        logger.info(f'源码保存路径为{source_path}')
        allure.attach.file(
            source_path,
            name='源码',
            attachment_type=allure.attachment_type.TEXT
        )
        return source_path

    def wait(self, method, no_time=6):
        """
        等待
        :param method:
        :param no_time:
        :return:
        """
        wait = WebDriverWait(self.driver, no_time).until(
            lambda x: x(AppiumBy.XPATH, f"//*[text='{method}']")
        )
        return wait

    def wait_im(self, n_time=15):
        """
        隐式等待
        :param n_time:
        :return:
        """
        self.driver.implicitly_wait(n_time)

    @black
    def find_ele_click(self, by, value=None):
        """
        查找元素并点击
        :param by: 定位方式
        :param value: 定位表达式
        """
        if value == None:
            self.driver.find_element(*by).click()
        else:
            self.driver.find_element(by, value).click()

    @black
    def find_ele_key(self, by, send_key, value=None):
        """
        查找元素并点击
        :param by: 定位方式
        :param value: 定位表达式
        """
        if value == None:
            ele3 = self.driver.find_element(*by).send_keys(send_key)
        else:
            ele3 = self.driver.find_element(by, value).send_keys(send_key)
        return ele3

    @black
    def find_ele_text(self, by, value=None):
        """
        查找元素并点击
        :param by: 定位方式
        :param value: 定位表达式
        """
        if value == None:
            ele3 = self.driver.find_element(*by).text
        else:
            ele3 = self.driver.find_element(by, value).text
        return ele3
